package com.itheima.crawler;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.net.URLEncoder;

/**
 * 使用jsoup 爬数据
 */
public class JsoupYCrawler {
    public static void main(String[] args) throws Exception {
        //http://www.jinmalvyou.com/search/index/view_type/1/keyword/%E5%9B%BD%E5%86%85%E6%B8%B8/p/1.html
        String tempStr = "国内";
        String encode = URLEncoder.encode(tempStr, "utf-8");
        System.out.println(encode);
        //网站的地址
        Document document = Jsoup.connect("http://www.jinmalvyou.com/search/index/view_type/1/keyword/" + tempStr + "/p/1.html").get();

        //数据获得部分
        Elements allLiElements = document.select(".rl-b2 .rl-b-li");//获得第一页所有的li

        //遍历数据
        for (Element curLiElement : allLiElements) { //current 当前的li元素
            //li对象
            //获得名称
            Elements aElement = curLiElement.select(".pro-title a");//获得rname的a标签
            //System.out.println(aElement);
            String rname = aElement.text();
            //System.out.println(rname);

            //获得描述
            Elements descElement = curLiElement.select(".pro-title+br+p");
            //获得title属性值
            //System.out.println(descElement.attr("title"));
            String desc = descElement.attr("title");

            //获得价格
            Element priceElement = curLiElement.select(".pro-price strong").first();
            String price = priceElement.text();
            System.out.println(price);
            //获得图片
        }
    }
}
